package com.peas.platform.module.icims.contract.basal.domain.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import com.peas.platform.module.icims.contract.basal.adapter.repository.ContractTypeRepository;
import com.peas.platform.module.icims.contract.basal.adapter.repository.po.ContractTypePo;
import com.peas.platform.module.icims.contract.basal.domain.ContractTypeDomain;
import com.peas.platform.module.icims.contract.basal.dto.ContractTypeDto;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component
public class ContractTypeDomainImpl extends BaseDomainImpl<ContractTypeDto, ContractTypePo> implements ContractTypeDomain {

    @Resource
    private ContractTypeRepository contractTypeRepository;
    @Resource
    private SequenceService sequenceService;

    @Override
    public BaseRepository<ContractTypePo> getMapper() {
        return contractTypeRepository;
    }

    @Override
    public Class<? extends BaseRepository<ContractTypePo>> getRepositoryClass() {
        return ContractTypeRepository.class;
    }

    @Transactional
    @Override
    public int save(ContractTypeDto dto) {
        if (StrUtil.isBlank(dto.getCode())) {
            String code = sequenceService.genSequence("base", "ContractTypeDto", "CTD", 4, 1);
            dto.setCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("code", dto.getCode());
        return result;
    }

    @Transactional
    @Override
    public int updateById(ContractTypeDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("code", dto.getCode());
        return result;
    }

}
